<template>
    <view class="home-reset-dev">
        <view class="set-mod">
            <view class="mod-body">
                <view class="tot-item">
                    <text class="item-lab">远程开关机</text>
                </view>
            </view>
            <!--<view class="mod-foot">-->
                <!--<view class="set-number-box">-->
                    <!--<u-number-box v-model="reset_days" @change="valChange" :min="0" :max="10000" :step="1" :positive-integer="false" ></u-number-box>-->
                <!--</view>-->
            <!--</view>-->

        </view>
        <view class="u-padding-bottom-10 u-flex">
            <u-button type="success" size="medium" @click="dev_set(1)">开启</u-button>
            <u-button type="warning" size="medium" @click="dev_set(0)">关闭</u-button>
        </view>
    </view>
</template>

<script>
    export default {
        components: {},
        data() {
            return {
				reset_days_val: -1,
                reset_days: 0,
            };
        },
        props: {
            device:{
                type:Object,
                default:function(){
                    return {};
                }
            }
        },
        computed: {},
        created() {
            // this.reset_days = parseFloat(this.device.reset_days);
			// this.reset_days_val = parseFloat(this.device.reset_days);
        },
        methods: {

            // valChange(e) {
            //     console.log('当前值为: ' + e.value);
            //     // this.reset_days_val = e.value
            // },

            dev_set(opt){

                // if(parseFloat(this.reset_days_val) < 0){
                //     uni.showModal({
                //         title:"系统提醒",
                //         content:'天数必须大于0',
                //         showCancel:false
                //     });
                //     return false;
                // }

                uni.showModal({
                    title:"系统提醒",
                    content: opt === 1 ? '确认开启净水器' : '确认关闭净水器',
                    success: res => {
                        if (res.confirm) {

                            let message = {};
                            // message.device_id = this.device.device_id;
                            // message.command = opt === 1 ? 'open' : 'close';
                            message.days = this.reset_days_val;//天数，APP端永远发-1即可
                            let command = opt === 1 ? 'open' : 'close';
                            console.log("set days:" + message.days + " for " + command);

                            /* 走API接口，重启设备 */
                            this.$post("/api/repair.index/oper_shebei?command="+command+"&device_id=" + this.device.device_id, message).then(res => {
                                if(res.code == 1){
                                    this.$u.toast("操作成功");
                                }else{
                                    this.$u.toast("操作失败");
                                }
                            });

                            /* 下面这个接口也可以，但是这个接口只能后台使用，前台用不了 */
                            // this.$post("repair/shebei/oper_shebei?command="+command+"&device_id=" + this.device.device_id, message).then(res => {
                            //     if(res.code == 1){
                            //         this.$u.toast("操作成功");
                            //     }else{
                            //         this.$u.toast("操作失败");
                            //     }
                            // });
                        }
                    },
                    fail: err => {
                        console.log(`%cuni.showModal取消：`, 'do nothing for open/close device');
                    }
                });
            }
        }
    };
</script>


<style lang="scss" scoped>
    .home-reset-dev{
        margin: 24upx;
        box-shadow: -1upx 2upx 20upx 0 rgba(6, 0, 1, 0.06);
        border-radius: 10upx;
        background-color: #fff;
    }
    .set-mod{
        position: relative;
        display: flex;
        align-items: center;
        padding: 24upx;
        min-height: 88upx;
        line-height: 1.2;


        .mod-body{
            flex: 1;
            min-width: 0;
        }
        .mod-foot{
            margin-left: 24upx;
            display: flex;
            align-items: center;
        }
    }
    .tot-item{
        font-size: 32upx;
        .item-lab{
            font-size: 26upx;
        }
        .item-num{

        }
        .item-unit{

        }
    }
    .set-number-box{
        /deep/.u-numberbox{
            .u-number-input{
                width: 108upx!important;
                height: 48upx!important;
                font-weight: bold;
                font-size: 34upx!important;
                color: #428cff!important;
            }
            .u-icon-minus,
            .u-icon-plus{
                width: 48upx!important;
                height: 48upx!important;
                border-radius: 14upx;
            }
            .u-icon-minus{
                color: #535455!important;
                background-color: #f4f6fa!important;
            }
            .u-icon-plus{
                color: #428cff!important;
                background-color: #dbeaff!important;
            }
        }
    }
    .set-btns{
        margin-left: 30upx;
        /deep/.u-btn{
            &.u-size-mini{
                font-size: 24upx;
            }
        }
    }
</style>